/*
 * @Author: your name
 * @Date: 2022-01-20 14:51:53
 * @LastEditTime: 2022-01-20 14:51:54
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \AROperationManager_web\src\utils\http.js
 */
/*
 * @Author: your name
 * @Date: 2020-06-08 10:54:35
 * @LastEditTime: 2022-01-20 14:27:22
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \代码管理\assist\src\assets\api\http.js
 */
import Axios from 'axios'
// import Cookies from 'js-cookie'
import router from '@/router';
import { Message } from 'element-ui'
const IP = window.IP
// import TokenKeys from './sessionKey'
import qs from 'qs'

// 用于取消请求
var custom = null

// const TokenKeys = Tokens.getSessionKey()

// let cookieKey = TokenKeys.Cookie

let ROOT // 目的是在开发环境中能够跨域请求文件
if (process.env.NODE_ENV === 'development') {
  console.log('开发环境')
  ROOT = '/web'

} else if (process.env.NODE_ENV === 'production') {
  console.log('生成环境')
  ROOT = IP.httpsUrl
}

const http = Axios.create()
http.defaults.timeout = 1 * 60 * 1000

// // 添加请求拦截
// http.interceptors.request.use((config) => {
//   // 请求前可以做一些事
//   return config
// }, function (error) {
//   // 请求错误做的事
//   return Promise.reject(error)
// })

// // 响应拦截
// http.interceptors.response.use(
//   response => {
//     if (response.status === 200) {
//       let resData = response.data
//       switch (resData.code) {
//         case 401:
//           Message.error('登录过期，请重新登录')
//           setTimeout(() => {
//             // Cookies.remove(cookieKey) // 删除 无效的appToken
//             router.replace({
//               path: '/',
//               query: {
//                 redirect: router.currentRoute.fullPath
//               }
//             })
//           }, 500)
//           break
//         case 404:
//           Message.error('找不到请求页面')
//           break
//         case 200: {
//           // Message.success(resData.message)
//         }
//       }
//       return Promise.resolve(response)
//     } else {
//       Message.error('请求返回数据错误')
//       // eslint-disable-next-line no-undef
//       return Promise.resolve(response)
//     }
//   },
//   error => {
//     return Promise.reject(error)
//   })

/**
 * 公共区域设置
 */
function getConHttpObj (methods, urls) {
  return {
    method: methods,
    url: urls,
    params: null,
    data: null,
    headers: {
      'Content-Type': 'application/json;charset=utf-8',
      'Authorization': ''
    }
  }
}

const getRequest = (data) => {
  // const token = sessionStorage.getItem('manageToken')
  let newUrl = ROOT + data.url
  let conObj = getConHttpObj('get', newUrl)
  conObj.params = data.data
  if (token) {
    conObj.headers.Authorization = token
  }
  return http(conObj)
}

//  参数在 url 拼接上的
const getRequestID = (data) => {
  // const token = sessionStorage.getItem('manageToken')
  let newUrl = ROOT + data.url
  let conObj = getConHttpObj('get', newUrl)
  conObj.params = data.data
  // if (token) {
  //   conObj.headers.Authorization = token
  // }
  return http(conObj)
}



// post请求
const postRequest = (data) => {
  // const token = sessionStorage.getItem('manageToken')
  let newUrl = ROOT + data.url
  let conObj = getConHttpObj('post', newUrl)
  conObj.data = data.data
  if (token) {
    conObj.headers['Content-Type'] = 'application/json;charset=UTF-8'
    conObj.headers.Authorization = token
  }
  return http(conObj)
}

// post form表单形式
const postRequestForm = (data) => {

  // const token = sessionStorage.getItem('manageToken')
  let newUrl = ROOT + data.url
  let dataForm = deleObjToForm(data.data)
  let conObj = getConHttpObj('post', newUrl)
  conObj.data = dataForm
  if (token) {
    conObj.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
    console.log(conObj.headers['Content-Type'])
    conObj.headers.Authorization = token
  }
  return http(conObj)
}

function deleObjToForm (obj) {
  if (obj.constructor === Object) {
    let keys = Object.keys(obj)
    let formData = new FormData()
    keys.forEach(tt => {
      formData.append(tt.toString(), obj[tt])
    })
    return formData
  } else {
    throw new Error('必须传递对象')
  }
}

// post query形式
const postRequestQuery = (data) => {
  // const token = sessionStorage.getItem('manageToken')
  let newUrl = ROOT + data.url
  let conObj = getConHttpObj('post', newUrl)
  conObj.data = updataArr(data.data)
  // if (token) {
  conObj.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
  //   conObj.headers.Authorization = token
  // }
  return http(conObj)

  /**
   * 用于序列化数组 变成 ids='ss'&ids='88'
   * @param {*} params {ids:['ss','88']}
   */
  function updataArr (params) {
    let ss = qs.stringify(params, { indices: false })
    console.log(ss)
    return ss
  }
}

// const postRequestQuerys = (data) => {
//   const token = sessionStorage.getItem('manageToken')
//   let newUrl = ROOT + data.url
//   let conObj = getConHttpObj('post', newUrl)
//   conObj.data = updataArr(data.data)
//   if (token) {
//     conObj.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
//     conObj.headers.Authorization = token
//     conObj.headers.Accept='*/*'
//   }
//   return http(conObj)

//   /**
//    * 用于序列化数组 变成 ids='ss'&ids='88'
//    * @param {*} params {ids:['ss','88']}
//    */
//   function updataArr (params) {
//     let ss = qs.stringify(params, { indices: false })
//     console.log(ss)
//     return ss
//   }
// }

// 返回 上传文件
const AddFilesImgs = (data, fn) => {
  custom = Axios.CancelToken.source()
  let newUrl = ROOT + data.url
  // const token = sessionStorage.getItem('manageToken')
  let conObj = getConHttpObj('post', newUrl)
  conObj.data = data.data
  conObj.onUploadProgress = fn
  conObj.cancelToken = custom.token
  if (token) {
    conObj.headers['Content-Type'] = 'multipart/form-data'
    conObj.headers.Authorization = token
  }
  return http(conObj)
}

const deleCance = (text) => {
  console.log('取消了')
  custom.cancel(text)
}

const getHttpUrlAndToken = (data) => {
  let newUrl = ROOT + data.url
  // const token = sessionStorage.getItem('manageToken')
  return {
    Url: newUrl,
    Authorization: token
  }
}

export default {
}








